package cn.iocoder.yudao.module.engine.controller.app.usertimetransaction;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.security.core.annotations.PreAuthenticated;
import cn.iocoder.yudao.module.engine.controller.app.usertimetransaction.vo.AppUserTimeTransactionPageReqVO;
import cn.iocoder.yudao.module.engine.controller.app.usertimetransaction.vo.AppUserTimeTransactionRespVO;
import cn.iocoder.yudao.module.resource.api.IUserTimeTransactionApi;
import cn.iocoder.yudao.module.resource.api.model.usertimetransaction.UserTimeTransactionDTO;
import cn.iocoder.yudao.module.resource.api.model.usertimetransaction.UserTimeTransactionPageReqDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Tag(name = "用户 APP - 用户时长交易记录")
@RestController
@RequestMapping("/lm/user-time-transaction")
@Validated
public class AppUserTimeTransactionController {
    @Resource
    private IUserTimeTransactionApi userTimeTransactionApi;

    @GetMapping("/get")
    @Operation(summary = "获得用户时长交易记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthenticated
    public CommonResult<AppUserTimeTransactionRespVO> getUserTimeTransaction(@RequestParam("id") String id) {
        final Long userId = getLoginUserId();
        UserTimeTransactionDTO userTimeTransaction = userTimeTransactionApi.getUserTimeTransaction(userId, id);
        return success(BeanUtils.toBean(userTimeTransaction, AppUserTimeTransactionRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户时长交易记录分页")
    @PreAuthenticated
    public CommonResult<PageResult<AppUserTimeTransactionRespVO>> getUserTimeTransactionPage(@Valid AppUserTimeTransactionPageReqVO pageReqVO) {
        final Long userId = getLoginUserId();
        final UserTimeTransactionPageReqDTO req = BeanUtils.toBean(pageReqVO, UserTimeTransactionPageReqDTO.class);
        req.setUserId(userId);

        PageResult<UserTimeTransactionDTO> pageResult = userTimeTransactionApi.getUserTimeTransactionPage(req);
        return success(BeanUtils.toBean(pageResult, AppUserTimeTransactionRespVO.class));
    }

}